import AV from '../request.js'

// Sentence 句子
import axios from 'axios'

function loadingFun(_t) {
  return _t.$loading({
    lock: true,
    text: 'Loading',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
}

function push(obj,meClass,_t) {
  let loading = null
  if(_t){
    loading = loadingFun(_t)
  }

  // console.log(obj,meClass)
  return new Promise((resolve, reject) => {

    // 声明 class
    const Sentence = AV.Object.extend(meClass);
    // 构建对象
    let sentence = new Sentence();
    // sentence = {
    //   ...sentence,
    //   ...obj
    // }
    // 为属性赋值
    // console.log(meClass,'存到',obj)
    for (const iterator of Object.keys(obj)) {
      // console.log(iterator)
      sentence.set(iterator, obj[iterator]);
    }
    // 将对象保存到云端
    sentence.save().then((sentence) => {
      // 成功保存之后，执行其他逻辑
      // console.log(`保存成功。objectId：${sentence.id}`);
      resolve(sentence)
      loading && loading.close();
    }, (error) => {
      reject()
      loading && loading.close();
      // 异常处理
    });

  })
}
// 61f4af4954971e406ad925a3

function getId(objectId,meClass = 'Sentence') {
  const query = new AV.Query(meClass);
  query.get(objectId).then((todo) => {
    // console.log('获取到的 内容',todo.attributes)
  }, (error) => {
    loading.close();
    // 异常处理
  });
}
// getId('61f4af4954971e406ad925a3')

function update(objectId,obj,meClass = 'Sentence',_t) {
  return new Promise((resolve, reject) => {
    let loading = null
    if(_t){
      loading = loadingFun(_t)
    }

    console.log(objectId,obj)
    const todo = AV.Object.createWithoutData(meClass, objectId);
    // todo.set('content', content);
    console.log(meClass,'存到',objectId,obj)
    for (const iterator of Object.keys(obj)) {
      // console.log(iterator)
      todo.set(iterator, obj[iterator]);
    }
    todo.save().then((todo) => {
      console.log('修改后 内容',todo.attributes)
      resolve(todo.attributes)
      loading.close();
    }, (error) => {
      loading.close();
      // 异常处理
    });

  })
}
// new Date().getTime() + ''
// update('61f4af4954971e406ad925a3',new Date().getTime() + '')

function getPage(data = {
  page:1,
  pageSize:10
},meClass = 'Sentence') {
  return new Promise((resolve, reject) => {
    const query = new AV.Query(meClass);
    if (data.pageSize) {
      // 查多少条
      query.limit(data.pageSize);
      // 跳过前面属性
      query.skip(data.pageSize * (data.page - 1));
    }
    // 属性
    Object.keys(data).forEach(element => {
      // console.log(element)
      if (element != 'pageSize' && element != 'page' ) {
        query.equalTo(element, data[element]);
      }
    });

    // students 是包含满足条件的 Student 对象的数组
    query.find().then((students) => {
      query.count().then((count) => {
        // console.log(`${count} 个 todo 已完成。`);
        resolve({
          list:students,
          count:count
        })
      });
    });
  });
}

// getPage({
//   page:1,
//   pageSize:10
// }).then((res)=> {
//   console.log('前十条',res)
// })


function deleteFun(objectId,meClass = 'Sentence') {
  return new Promise((resolve, reject) => {
    const todo = AV.Object.createWithoutData(meClass, objectId);
    todo.destroy();
    resolve()
  })
}

// var paramsJson = {
//   url:'',
//   header:'Bearer '+ this.internalData.tenant_access_token,
//   data:{
//     ranges:['0b1422!A1:CZ999']
//   }
// };
import { Loading,Message } from 'element-ui';
function postGet(paramsJson) {
  return new Promise((resolve, reject) => {

    const loading = Loading.service({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    });

    AV.Cloud.run('wangdefa', {
      ...paramsJson
    }).then(function(data) {
      // 调用成功，得到成功的应答 data
      // console.log(' wangdefa 调用成功 ',data)
      loading.close();
      if(data.statusCode != 200){
        if (data.statusCode == 403) {
          Message('当前获取表格没有权限调取数据')
        }else{
          Message(JSON.parse(data.body).msg)
        }
        reject({})
        return
      }
      resolve(JSON.parse(data.body))
    }, function(err) {
      // 处理调用失败
      Message('连接超时请刷新页面重新登录');
      console.log('处理调用失败 ',err)
      loading.close();
      reject(err)
    });
  })
}

export default {
  getPage,
  update,
  getId,
  deleteFun,
  push,
  postGet
}
